import { login, logout } from "@/api/login";
import { getUserInfo } from "@/api/system/user"
import {
	getToken,
	setToken,
	removeToken,
	getUser,
	setUser,
	removeUser,
	removeClubAndUser,
} from "@/utils/auth";
import { defineStore } from "pinia";
const useUserStore = defineStore("user", {
	state: () => ({
		userName: getUser()?.userName || null,
		avatar:getUser()?.avatar || null,
		roles: Array(),
		permissions: [],
		userId: getUser()?.userId || null,
		nickName: getUser()?.nickName || '暂未登录',
		// sex: getUser()?.sex || '未知',
		phone:getUser()?.phone || '暂无',
		createTime: getUser()?.createTime || '暂无',
		roleId: getUser()?.roleId || null,
		balance: getUser()?.balance || null,
		realName: getUser()?.realName || '暂无',
		memberId: getUser()?.memberId || null,
		isMerchant: getUser()?.isMerchant || null,
		isMember: getUser()?.isMember || null,
		clueNumber: getUser()?.clueNumber || null,
		deposit: getUser()?.deposit || null,
		shareClue:getUser()?.shareClue || null,
		superClue:getUser()?.superClue || null,
		
		memberCreateTime: getUser()?.memberCreateTime || null,
		memberTotalValidity: getUser()?.memberTotalValidity || 0,
		isLogin: getToken() ? true : false,	
		showLoginPopup: false,
	}),
	actions: {
		// 获取用户信息
		getInfo(userId: any) {
			return new Promise((resolve, reject) => {
				getUserInfo(userId)
					.then(async (res: any) => {
						const user = res.data;
						this.isLogin = true;
						const avatar = user.avatar == "" || user.avatar == null ? "" : user.avatar;
						if (res?.data?.sysUser?.roles && res?.data?.sysUser?.roles?.length > 0) {
							// 验证返回的roles是否是一个非空数组
							this.roles = res?.data?.sysUser?.roles;
							this.permissions = res?.data?.permissions;
							this.roleId = res?.data?.sysUser?.roles[0]?.roleId;

						} else {
							this.roles = ["ROLE_DEFAULT"];
						}
						if (user) {
							for (const key in user) {
									(this as any)[key] = user[key];
								}
						}
						setUser(this.$state);
						resolve(res);
					})
					.catch((error) => {
						// const storageUser = getUser();
						// if (storageUser) {
						// 	for (const key in storageUser) {
						// 		if (key in this) {
						// 			(this as any)[key] = storageUser[key];
						// 		}
						// 	}
						// }
						reject(error);
					});
			});
		},
		// 退出系统
		logOut() {
			return new Promise<null>((resolve, reject) => {
				logout()
					.then(() => {
						this.userName = null;
						this.avatar = null;
						this.roles = Array();
						this.permissions = [];
						this.userId = null;
						this.nickName = '暂未登录';
						// this.sex = '未知';
						this.phone = '暂无';
						this.createTime = '暂无';
						this.roleId = null;
						this.balance = null;
						this.realName = '暂无';
						this.memberId = null;
						this.isMerchant = null;
						this.isMember = null;
						this.clueNumber = null;
						this.deposit = null;
						this.shareClue = null;
						this.superClue = null;





						this.isLogin = false;
						removeToken();
						removeUser();
						removeClubAndUser();
						resolve(null);
					})
					.catch((error) => {
						reject(error);
					});
			});
		},
		 // 显示登录弹窗
		 showLogin() {
			this.showLoginPopup = true
		  },
		  // 隐藏登录弹窗
		  hideLogin() {
			this.showLoginPopup = false
		  },
		  handleAuthFail() {
			this.showLogin()
		  }
	},
});

export default useUserStore;